/*
 * author      : 吴中勤 
 * date        : 2018/11/11
 * description : TODO
 */

package org.wzq.learn;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.wzq.learn.threadpool.AppRuntimeEnvThread;
import org.wzq.learn.threadpool.ThreadPoolService;

@RestController
@RefreshScope
public class TradeController {

    private static final Logger logger = LoggerFactory.getLogger(TradeController.class);

    @RequestMapping(value = {"/ruok"}, method = RequestMethod.GET)
    public String hello2() {
        return "imok";
    }

    @Autowired
    private ThreadPoolService threadPoolService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private TradeService tradeService;

    @GetMapping(value = "/hi-shop")
    public String hiShop(@RequestParam String name) {
        // here traceContext traceId
        return restTemplate.getForObject("http://shop-service/shop/ruok?name=" + name, String.class);
    }

    @RequestMapping(value = "/thread")
    public String thread() {
        // traceId
        threadPoolService.executor(() -> {
            String shopResult = restTemplate.getForObject("http://shop-service/shop/ruok?name=a", String.class);
            if (logger.isInfoEnabled()) logger.info("shop-result=" + shopResult);
            try {
                Thread.sleep(10 * 1000);
                AppRuntimeEnvThread t = (AppRuntimeEnvThread) Thread.currentThread();
                if (logger.isInfoEnabled()) logger.info("in thread, appRuntimeEnv=" + t.getAppRuntimeEnv().toString());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (logger.isInfoEnabled()) logger.info("thread done");
        });
        return "imok";
    }


    @RequestMapping(value = "/thread2")
    public String thread2() {
        // traceId
        threadPoolService.executor(() -> {
            // 1. 线程中的异常, 直接上报 sentry
            if (logger.isInfoEnabled()) logger.info("in thread, threadId=" + Thread.currentThread().getId());
            throw new RuntimeException("thread2 exception");

            // 2. TODO 如何解决 线程中调用其他服务, 传递threadlocal的问题
            // String shopResult = restTemplate.getForObject("http://shop-service/shop/ruok?name=a", String.class);
        });
        return "imok";
    }

    // 使用线程调用, 测试 trace 是否还正常
    @GetMapping(value = "/hi-shop2")
    public String hiShop2(@RequestParam String name) {
        return tradeService.hiShopInThread(name);
    }

    @GetMapping(value = "/shop-wrong")
    public String hiShop() {
        return restTemplate.getForObject("http://shop-service/shop/wrong", String.class);
    }

    // 从公共配置中获取
    @Value("${author:null}")
    private String author;

    // 从 applicationName-profile中获取
    @Value("${trade-name:null}")
    private String tradeName;

    @RequestMapping(value = "/hi-config")
    public String hi() {
        return "author=" + author + ", tradeName=" + tradeName;
    }
}
